package com.cgc999.host.adapter;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过show hide 显示Fragment的适配器，可以延时加载，只有使用AbsFragment才有用
 */
public class FragmentsReplaceAdapter {
    private FragmentManager mFragmentManager = null;
    private List<PageInfo> mTabs = null;
    private int containerId = 0;
    private Context mContext = null;
    private Fragment mCurrentPrimaryItem = null;
    private OnPageChangeListener onPageChangeListener;
    private int mCurrentPageIndex = 0;
    private List<Fragment> mList = new ArrayList<>();


    public FragmentsReplaceAdapter(FragmentManager manager, int contentId, Context context) {
        this.mFragmentManager = manager;
        this.containerId = contentId;
        mContext = context;
        mTabs = new ArrayList<>();
    }

    /**
     * @param tag
     * @param clasz
     * @param bundle
     */
    public void addTag(String tag, Class<? extends Fragment> clasz, Bundle bundle) {
        mTabs.add(new PageInfo(null, tag, clasz, bundle));
    }

    private Fragment getItem(int position) {
        PageInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    public void addFragment(Fragment fragment){
//        mList.add(fragment);
        mTabs.add(new PageInfo(null,fragment.getTag(),fragment.getClass(),fragment.getArguments()));
    }
//    private Fragment getFragment(int position){
//        return mList.get(position);
//    }



    public long getItemId(int position) {
        return position;
    }
    //    private void setPrimaryItem(Fragment fragment) {
    //        if (fragment != mCurrentPrimaryItem) {
    //            if (mCurrentPrimaryItem != null) {
    //                mCurrentPrimaryItem.setMenuVisibility(false);
    //                mCurrentPrimaryItem.setUserVisibleHint(false);
    //            }
    //            if (fragment != null) {
    //                fragment.setMenuVisibility(true);
    //                fragment.setUserVisibleHint(true);
    //            }
    //            mCurrentPrimaryItem = fragment;
    //        }
    //    }
    //    private Fragment instantiateItem(int containerId, int position) {
    //        final long itemId = getItemId(position);
    //        // Do we already have this fragment?
    //        String name = makeFragmentName(containerId, itemId);
    //        Fragment fragment = mFragmentManager.findFragmentByTag(name);
    //        if (fragment == null) {
    //            fragment = getItem(position);
    //            FragmentTransaction transaction = mFragmentManager.beginTransaction();
    //            transaction.add(containerId, fragment, makeFragmentName(containerId, itemId));
    //            transaction.commitAllowingStateLoss();
    //        }
    //        return fragment;
    //    }

    public Fragment getCurrentItem() {
        return mCurrentPrimaryItem;
    }

    public int getCurrentPageIndex() {
        return mCurrentPageIndex;
    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }

    public void setCurrentItem(int position){
        setCurrentItem(position,null);
    }
    public void setCurrentItem(int position,Bundle bundle) {
        mCurrentPageIndex = position;
        final long itemId = getItemId(position);
        // Do we already have this fragment?
        String name = makeFragmentName(containerId, itemId);
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null && fragment == mCurrentPrimaryItem) {
//            fragment.setArguments(bundle);
            return;
        }
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (fragment == null) {
            fragment = getItem(position);
            transaction.add(containerId, fragment, makeFragmentName(containerId, itemId));
        }
        if (mCurrentPrimaryItem != null) {
            transaction.hide(mCurrentPrimaryItem);
        }
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        transaction.show(fragment);
        transaction.commitAllowingStateLoss();
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageChangeAfter(position);
        }
    }

    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    public interface OnPageChangeListener {
        public void onPageChangeAfter(int position);
    }
}
